package com.ujiuye.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * CompanyName: IT优就业<br/>
 * Datetime: 2021/7/1 10:45<br/>
 *
 * @author TanJian
 */
public abstract class BaseServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //1:获取方法名
        String method =   req.getParameter("method");

        //2:利用反射获取Class文件
        Class<? extends BaseServlet> clazz = this.getClass();
        //3:获取Method类
        try {
            Method mt = clazz.getDeclaredMethod(method, HttpServletRequest.class, HttpServletResponse.class);
            mt.setAccessible(true);
            //4：调用方法
            mt.invoke(this,req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
